package com.guyaning.deyson_core.core.wechat;

import com.alibaba.fastjson.JSONObject;
import com.guyaning.deyson_core.core.net.RestClient;
import com.guyaning.deyson_core.core.net.callback.IError;
import com.guyaning.deyson_core.core.net.callback.ISuccess;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

/**
 * Created by Administrator on 2018/5/10.
 */

public abstract class BaseWXEntryActivity extends BaseWXActivity {

    //用户登录成功之后的回调
    protected abstract  void  onSignInSuccess(String userInfo);

    //微信发给第三方应用的回调
    @Override
    public void onReq(BaseReq baseReq) {

    }

    //第三方应用发给微信的回调
    @Override
    public void onResp(BaseResp baseResp) {

        final String code = ((SendAuth.Resp)baseResp).code;
        final StringBuilder authUrl = new StringBuilder();

        authUrl.append("https://api.weixin.qq.com/sns/oauth2/access_token?appid=")
                .append(LatteWeChat.APP_ID)
                .append("&secret=")
                .append(LatteWeChat.APP_SECRET)
                .append("&code=")
                .append(code)
                .append("&grant_type=authorization_code");

        getAuth(authUrl.toString());

    }

    private void getAuth(final String authUrl) {

        RestClient.builder()
                  .url(authUrl)
                  .success(new ISuccess() {
                      @Override
                      public void onSuccess(String response) {
                          //获取必要的参数
                          final JSONObject authObj = JSONObject.parseObject(response);
                          final String accessToken = authObj.getString("access_token");
                          final String openId  = authObj.getString("openid");

                          final StringBuilder userInfoUrl = new StringBuilder();

                          userInfoUrl.append("https://api.weixin.qq.com/sns/userinfo?access_token=")
                                     .append(accessToken)
                                     .append("&openid=")
                                     .append(openId)
                                     .append("&lang=")
                                     .append("zh_CN");

                          getUserInfo(userInfoUrl.toString());
                      }
                  })
                  .build()
                  .get();
    }

    private void getUserInfo(String userInfoUrl) {

        RestClient.builder()
                  .url(userInfoUrl)
                  .success(new ISuccess() {
                      @Override
                      public void onSuccess(String response) {
                           onSignInSuccess(response);
                      }
                  })
                  .error(new IError() {
                      @Override
                      public void onError(int code, String msg) {

                      }
                  })
                  .build()
                  .get();

    }
}
